package com.aco.party.web.controller.system;

import com.aco.party.model.po.Comments;
import com.aco.party.model.vo.CommentsParam;
import com.aco.party.model.vo.CommentsVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.CommentsService;
import com.aco.party.service.system.MetaService;
import com.aco.party.web.command.system.EditCommentsCommand;
import com.aco.party.web.validator.system.EditCommentsValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import static com.aco.party.util.WebModelUtil.*;
/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManageCommentsController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ManageCommentsController.class);

    @Resource
    private CommentsService commentsService;

    @Resource
    private MetaService metaService;

    private EditCommentsValidator validator = new EditCommentsValidator();

    private final String REDIRECTEDIT = "redirect:edit";

    @RequestMapping(value = {"/comments", "/comments/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manageComments";
    }

    @RequestMapping(value = "comments/list", method = RequestMethod.POST)
    @ResponseBody
    public Page
            <CommentsVo> getListJson(HttpServletRequest request, DataGridEntry entry,String content) {
        Page <CommentsVo> page = new Page<>();
        entry.webParamConvert();
        CommentsParam param = new CommentsParam();
        param.setContent(content);
        List <CommentsVo> list = commentsService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),param);
        page.setTotal(commentsService.getTotal(param));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/comments/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response,@ModelAttribute EditCommentsCommand command, Model model) {
        if (command.getId() != null) {
            Comments po = commentsService.getByPrimaryKey(command.getId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
        }
        return "system/editComments";
    }

    @RequestMapping(value = "/comments/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute EditCommentsCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Comments comments = new Comments();
                command.toPo(comments);
            comments.setCommentAuthor(getCurrentUser().getId());
            comments.setUpdateTime(new Date());
            commentsService.update(comments);
            model.addAttribute("no_error",true);
        }
        return "system/editComments";
    }

    @RequestMapping(value = "/comments/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditCommentsCommand command) {
        command.setCommentApproved(1);
        return "system/editComments";
    }

    @RequestMapping(value = "/comments/add", method = RequestMethod.POST)
    public String add(Model model, @ModelAttribute EditCommentsCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editComments";
        }
        Comments comments = new Comments();
        command.toPo(comments);
        comments.setCommentAuthor(getCurrentUser().getId());
        comments.setCreateTime(new Date());
        comments.setUpdateTime(new Date());
        commentsService.add(comments);
        attributes.addAttribute("id",comments.getId());
        attributes.addFlashAttribute("no_error", true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/comments/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        commentsService.delete(id);
        return new Response();
    }

    @RequestMapping(value = "/comments/changeStatus",method = RequestMethod.POST)
    @ResponseBody
    public Response changeStatus(@RequestParam(required = true) Long id,Integer approved){
        Comments comments = new Comments();
        comments.setId(id);
        comments.setCommentApproved(approved);
        comments.setUpdateTime(new Date());
        commentsService.update(comments);
        return new Response();
    }

    private void setAtt(Model model){

    }

}
